<?php
namespace  app\manage\model;
use think\Db;
use think\Model;
class RoleAction extends Model{
    protected  $table = 'role_action';

    /**
     * @param $data
     * @return int|string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * 编辑
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($id, $data) {
        $data = Db::name($this->table)
            ->where(['role_id'=>$id])
            ->update($data);
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_delete($id) {
        $data = Db::name($this->table)
            ->where(['id'=>$id])
            ->delete();
        return $data;
    }

    /**
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id) {
        $data = Db::name($this->table)
            ->where(['role_id'=>$id])
            ->field('role_id, action_id')
            ->find();
        return $data;
    }

    /**
     * 完全权限路径
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail_q($id) {
        $data = Db::name($this->table)
            ->where(['role_id'=>$id])
            ->field('role_id, action_id, all_action')
            ->find();
        return $data;
    }

    /**
     * @param $role_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getActionRelation($role_id) {
        $data = Db::name($this->table)
            ->where(['role_id'=>$role_id])
            ->field('role_id, action_id')
            ->find();
        return $data;
    }

    /**
     * @param $role_id
     * @param $action_id
     * @return int|string
     */
    public function setActionRelation($role_id, $action_id, $all_permission) {
        $data['role_id'] = $role_id;
        $data['action_id'] = $action_id;
        $data['all_action'] = $all_permission;
        return $this->m_insert($data);
    }

    /**
     * @param $role_id
     * @param $action_id
     * @return int|string
     */
    public function updateActionRelation($role_id, $action_id, $all_permission) {
        $data['action_id'] = $action_id;
        $data['all_action'] = $all_permission;
        return $this->m_update($role_id, $data);
    }
}